package dshop.model.commission;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

import com.jfinal.plugin.activerecord.Db;

import appbase.jfinal.ext.BaseModel;
import appbase.kit.encrypt.MD5Kit;
import appbase.utils.DateUtil;

public class CommissionDetail extends BaseModel<CommissionDetail> {

	private static final long serialVersionUID = -6967680902549117464L;

	public static final CommissionDetail dao = new CommissionDetail();

	public static void modifyDesc(String descs, int changeRefId, int changeType) {
		Db.update(" update sp_commission_detail set descs = " + descs + ",status = 3 where changeType=" + changeType
				+ " and changeRefId=" + changeRefId + "");
//		dao.updateColumnBy("descs", "change_type, change_ref_id", descs, changeType, changeRefId);
	}

	public static boolean checkSign(int userId, BigDecimal money, BigDecimal moneyAfter, BigDecimal moneyBefore,
			int changeType, int changeRefId, Date createTime, String sign) {
		String newSign = generateCommissionDetailSign(userId, money, moneyAfter, moneyBefore, changeType, changeRefId,
				createTime);
		return newSign.equals(sign);
	}

	protected static String generateCommissionDetailSign(int userId, BigDecimal money, BigDecimal moneyAfter,
			BigDecimal moneyBefore, int changeType, int changeRefId, Date createTime) {
		StringBuilder builder = new StringBuilder();
		builder.append(money.setScale(2, RoundingMode.DOWN)).append("_sdfe_").append(userId).append("_dc*s_")
				.append(moneyAfter.setScale(2, RoundingMode.DOWN)).append("_").append(changeType).append("_2ck_")
				.append(moneyBefore.setScale(2, RoundingMode.DOWN)).append("_").append(changeRefId).append("_df34_")
				.append(DateUtil.dateFormat("yyyyMMdd", createTime));
		String sign = MD5Kit.encrptMD5(builder.toString());
		/*
		 * System.err.println("--------------------------------");
		 * System.err.println(builder.toString()); System.err.println("----");
		 * System.err.println(sign);
		 * System.err.println("--------------------------------");
		 */
		return sign;
	}

	protected static String generateCommissionDetailSign(CommissionDetail detail) {
		int userId = detail.getInt("user_id");
		BigDecimal money = detail.getBigDecimal("money");
		BigDecimal moneyAfter = detail.getBigDecimal("money_after");
		BigDecimal moneyBefore = detail.getBigDecimal("money_before");
		int changeType = detail.getInt("change_type");
		int changeRefId = detail.getInt("change_ref_id");
		Date createTime = detail.getDate("create_time");
		return generateCommissionDetailSign(userId, money, moneyAfter, moneyBefore, changeType, changeRefId,createTime);
	}

	public static CommissionDetail createCommissionDetail(int userId, BigDecimal money, BigDecimal moneyBefore,BigDecimal moneyAfter, String descs, int changeType, int changeRefId) {
		CommissionDetail detail = new CommissionDetail();
		detail.set("user_id", userId);
		detail.set("money", money);
		detail.set("money_before", moneyBefore);
		detail.set("money_after", moneyAfter);
		detail.set("descs", descs);
		detail.set("change_type", changeType);
		detail.set("change_ref_id", changeRefId);
		detail.set("create_time", new Date());

		String sign = generateCommissionDetailSign(detail);
		detail.set("sign", sign);

		return detail;
	}

}
